<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>{{ i18n('client.share.from') }}</div>
        </div>

        <div class="admin_main_block_right">
          <div>
            <el-button icon="Back" @click="router.go(-1)">
              {{ i18n('common.return') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_form_main">
        <el-form label-width="120px" ref="formRef" :model="dataForm" :rules="dataRule">
          <el-card class="box-card" shadow="never">
            <template v-slot:header>
              <div class="clearfix">
                <span>{{ i18n('client.basic_info') }}</span>
              </div>
            </template>
            <div class="text item">
              <el-row :gutter="20">
                <el-col :span="8">
                  <el-form-item :label="i18n('client.name')">
                    {{ dataForm.name }}
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="20">
                <el-col :span="8">
                  <el-form-item :label="i18n('client.current_username')">
                    <el-row style="width: 100%;">
                      <el-col :span="3">
                        <el-image class="cx-list-image" :src="dataForm.avatar" loading="lazy" lazy>
                          <template #error>
                            <div class="image-slot">
                              <el-icon><Picture /></el-icon>
                            </div>
                          </template>
                        </el-image>
                      </el-col>
                      <el-col :span="18">
                        {{ dataForm.nickname }}
                      </el-col>
                    </el-row>
                  </el-form-item>
                </el-col>
              </el-row>
            </div>
          </el-card>

          <el-card class="box-card mt10" shadow="never">
            <template v-slot:header>
              <div class="clearfix">
                <span>{{ i18n('client.share.from') }}</span>
              </div>
            </template>
            <div class="text item">
              <el-row :gutter="20">
                <el-col :span="8">
                  <el-form-item :label="i18n('contact.salesperson')" prop="user_id">
                    <el-select
                      v-model="dataForm.user_id"
                      :placeholder="i18n('common.please_select') + i18n('contact.salesperson')" multiple collapse-tags clearable>
                      <el-option v-for="(v, k) in userList" :label="v.nickname" :key="k" :value="v.id" :disabled="status[k]"></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
            </div>
          </el-card>

          <el-card class="box-card mt10" shadow="never">
            <div class="text item">
              <el-form-item>
                <el-button
                  type="primary"
                  @click="dataFormSubmit(formRef)">
                  {{ i18n('common.confirm') }}
                </el-button>
                <el-button @click="resetForm(formRef)">
                  {{ i18n('common.reset') }}
                </el-button>
              </el-form-item>
            </div>
          </el-card>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup name="client-share-form">
  import { useBase } from "@/hooks/base/useBase"
  import { useForm } from "@/hooks/form/useForm"

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('business/client')

  const userList = ref([])
  const status = ref([])

  const dataForm = reactive({
    id: 0,
    organization_id: '',
    user_id: [],
    current_user_id: '',
    name: '',
    nickname: '',
    avatar: '',
  })

  const dataRule = reactive({
    name: [{
      required: true,
      message: i18n('client.rules.name.require'),
      trigger: 'blur',
    }],
    level: [{
      required: true,
      message: i18n('client.rules.level.require'),
      trigger: 'blur',
    }],
  })

  const {
    formRef,
    resetForm
  } = useForm(model.value, dataForm)

  const init = (form) => {
    dataForm.id = query.id || 0

    nextTick(() => {
      form.value.resetFields()
      if (dataForm.id) {
        $http({
          url: $http.adornUrl(`/${model.value}/view/${dataForm.id}`),
          method: 'get',
          params: $http.adornParams({}),
        }).then(({ data }) => {
          if (data && data.data && data.status === 200) {
            dataForm.id = data.data.id
            dataForm.organization_id = data.data.organization_id
            dataForm.name = data.data.name

            if(data.data.user) {
              dataForm.current_user_id = data.data.user.id
              dataForm.nickname = data.data.user.nickname
              dataForm.avatar = data.data.user.avatar
            }
          }
        }).then(() => {
          if (dataForm.id) {
            $http({
              url: $http.adornUrl(`/${model.value}/share/select`),
              method: 'get',
              params: $http.adornParams({
                client_id: dataForm.id,
              }),
            }).then(({ data }) => {
              if (data && data.status === 200) {
                data.data.forEach((res) => {
                  dataForm.user_id.push(res.user_id)
                })
              }
            })
          }
        }).then(() => {
          if (dataForm.organization_id) {
            $http({
              url: $http.adornUrl(`/user/select`),
              method: 'get',
              params: $http.adornParams({
                organization_id: dataForm.organization_id,
              }),
            }).then(({ data }) => {
              if (data && data.status === 200) {
                data.data.forEach((res, k) => {
                  if(dataForm.current_user_id == res.id) {
                    status.value.push(true)
                  } else {
                    status.value.push(false)
                  }
                })

                userList.value = data.data
              }
            })
          }
        })
      }
    })
  }


  // 表单提交
  const dataFormSubmit = (form) => {
    form.validate((valid) => {
      if (valid) {
        $http({
          url: $http.adornUrl(`/${model.value}/share/handle`),
          method: 'post',
          data: $http.adornData({
            client_id: dataForm.id,
            user_id: dataForm.user_id,
          }),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            $message.success(i18n('common.handle_success'))
            router.go(-1)
          } else {
            $message.error(data.message)
          }
        })
      }
    })
  }

  onMounted(() => {
    init(formRef)
  })
</script>
